﻿package net.ycsk;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.nio.channels.SelectableChannel; 

import android.content.Intent;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Contacts.Intents.Insert;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.Button;
import android.widget.EditText;

import net.ycsk.widget.FileBrowser;
import net.ycsk.widget.OnFileBrowserListener;

import net.ycsk.model.newsModel;// 
import net.ycsk.bll.newsBLL;  //引用业务对象 

public class newsForm extends Activity implements OnClickListener,
		OnFileBrowserListener, OnMenuItemClickListener
{

	private FileBrowser fileBrowser;
	private View fileBrowserView;
	private AlertDialog alertDialog;	
	
    	private EditText etID; 
	private EditText etTitle; 
	private EditText etContent; 
	private EditText etHits; 
	private DatePicker dpAddDate; 
	private EditText etClassID; 
	private EditText etState; 
	private EditText etAuthor; 

	//private String photoFilename;

	newsBLL bll;
	private Integer ID = 0; // ID = ""

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		/*选择sd卡里的图片文件
		setContentView(R.layout.add_contact);
		Button btnSelectPhoto = (Button) findViewById(R.id.btnSelectPhoto);
		btnSelectPhoto.setOnClickListener(this);
		*/
		
		setContentView(R.layout.newsform);

		//控件初始化
				etID = (EditText) findViewById(R.id.newsFrom_etID); 
		etTitle = (EditText) findViewById(R.id.newsFrom_etTitle); 
		etContent = (EditText) findViewById(R.id.newsFrom_etContent); 
		etHits = (EditText) findViewById(R.id.newsFrom_etHits); 
		dpAddDate = (DatePicker) findViewById(R.id.newsFrom_dpAddDate); 
		etClassID = (EditText) findViewById(R.id.newsFrom_etClassID); 
		etState = (EditText) findViewById(R.id.newsFrom_etState); 
		etAuthor = (EditText) findViewById(R.id.newsFrom_etAuthor); 
		
		
		//---创建业务对象
		bll = new newsBLL(this);

		//如果是修改记录，就加载数据
		Intent intent = getIntent();
				Integer value = intent.getIntExtra("ID",0); 
		if (!value.equals(0)) { ; 
			ID = value ; 
      
        	newsModel model =  bll.selectOne(ID);        	
			        	etID.setText(model.getID().toString());  
        	etTitle.setText(model.getTitle());  
        	etContent.setText(model.getContent());  
        	etHits.setText(model.getHits().toString());  
        	//dpAddDate.setText(model.getAddDate());  
        	etClassID.setText(model.getClassID().toString());  
        	etState.setText(model.getState());  
        	etAuthor.setText(model.getAuthor());  

        }
	}


	@Override
	public void onDirItemClick(String path)
	{

	}	
	
	@Override
	//保存记录
	public boolean onMenuItemClick(MenuItem item)
	{ 
		//数据验证	 
		        	if (etID.getText().toString().equals("")) { 
        		etID.setText("0"); 
        	} 
        	if (etHits.getText().toString().equals("")) { 
        		etHits.setText("0"); 
        	} 
        	if (etClassID.getText().toString().equals("")) { 
        		etClassID.setText("0"); 
        	} 


		//创建一个类的实例
		newsModel model = new newsModel();
		
				model.setID(Integer.parseInt(etID.getText().toString())); 
		model.setTitle(etTitle.getText().toString()); 
		model.setContent(etContent.getText().toString()); 
		model.setHits(Integer.parseInt(etHits.getText().toString())); 
		//model.setAddDate(dpAddDate.getText().toString()); 
		model.setClassID(Integer.parseInt(etClassID.getText().toString())); 
		model.setState(etState.getText().toString()); 
		model.setAuthor(etAuthor.getText().toString()); 

				
		//调用插入方法		 	
		if(ID == 0){ // ID == ""
		    bll.insert(model);
		}else{
			model.setID(ID);
			bll.update(model);
		}
		setResult(RESULT_OK);
		finish();	
		return true;

		 //保存图片到数据库的例子
		//String sql = " insert into t_contacts(name, telephone, email, photo) values(?,?,?,?)";
		//ByteArrayOutputStream baos = new ByteArrayOutputStream();
		//((BitmapDrawable) ivPhoto.getDrawable()).getBitmap().compress(
		//		CompressFormat.JPEG, 50, baos);
		//Object[] args = new Object[]
		//{ etName.getText(), etTelephone.getText(), etEmail.getText(),
		//		baos.toByteArray() };
		//Main.dbService.execSQL(sql, args);
		//Main.contactAdapter.getCursor().requery();		
		//Main.contactAdapter.notifyDataSetChanged();
		//finish();		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add("保存").setOnMenuItemClickListener(this);
		return super.onCreateOptionsMenu(menu);
	}
	//下面两个方法与选择SD卡图片相关
	@Override
	public void onFileItemClick(String filename)
	{
		if (filename.toLowerCase().endsWith("jpg")
				|| filename.toLowerCase().endsWith("jpeg"))
		{
			try
			{
			   /*
				FileInputStream fis = new FileInputStream(filename);
				ivPhoto.setImageDrawable(Drawable.createFromStream(fis,
						filename));
				alertDialog.dismiss();
				photoFilename = filename;
				*/
			}
			catch (Exception e)
			{
				// TODO: handle exception
			}

		}
	}

	@Override
	public void onClick(View view)
	{
        /*
		fileBrowserView = getLayoutInflater().inflate(R.layout.select_photo,
				null);
		fileBrowser = (FileBrowser) fileBrowserView
				.findViewById(R.id.filebrowser);
		fileBrowser.setOnFileBrowserListener(this);
		alertDialog = new AlertDialog.Builder(this).setTitle("选择联系人头像")
				.setIcon(R.drawable.select_photo).setView(fileBrowserView)
				.setPositiveButton("关闭", null).create();
		alertDialog.show();
		*/
	}
    
}
